package com.tf8.treasure.controller;

import com.tf8.treasure.annotation.CheckLogin;
import com.tf8.treasure.common.Constant;
import com.tf8.treasure.domain.BaseRequest;
import com.tf8.treasure.domain.QueryTopUpInfoResponse;
import com.tf8.treasure.domain.RechargeOptionVO;
import com.tf8.treasure.entity.LtUser;
import com.tf8.treasure.entity.RechargeOption;
import com.tf8.treasure.entity.RechargeOptionExample;
import com.tf8.treasure.mapper.RechargeOptionMapper;
import com.tf8.treasure.service.MultiMediaService;
import com.tf8.treasure.utils.DataMask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/topup")
public class RechargeOptionController extends BaseController {
    @Autowired
    private RechargeOptionMapper rechargeOptionMapper;

    @RequestMapping(value = "/queryTopUpInfo", method = RequestMethod.POST)
    @CheckLogin
    public ResponseEntity queryTopUpInfo(@RequestBody BaseRequest baseRequest) throws Exception {
        QueryTopUpInfoResponse queryTopUpInfoResponse = new QueryTopUpInfoResponse();
        LtUser ltUser = getSession().getLtUser();
        queryTopUpInfoResponse.setPortrait(ltUser.getPortraitUrl());
        queryTopUpInfoResponse.setPhoneNo(DataMask.mask(3, 4, ltUser.getMobile()));
        queryTopUpInfoResponse.setBalance(ltUser.getBalance() + Constant.UNIT);
        List<RechargeOption> rechargeOptions = rechargeOptionMapper.selectByExample(new RechargeOptionExample());
        if (!CollectionUtils.isEmpty(rechargeOptions)) {
            List<RechargeOptionVO> options = new ArrayList<>();
            for (RechargeOption rechargeOption : rechargeOptions) {
                RechargeOptionVO rechargeOptionVO = new RechargeOptionVO();
                DecimalFormat df = new DecimalFormat("0.##");
                rechargeOptionVO.setAmount(df.format(rechargeOption.getAmount()) + "元");
                BigDecimal multiply = rechargeOption.getAmount().multiply(new BigDecimal(rechargeOption.getRatio()));
                rechargeOptionVO.setTips("兑换" + df.format(multiply) + Constant.UNIT);
                if(rechargeOption.getDiscount() !=0){
                    rechargeOptionVO.setSaleTips("再送" + rechargeOption.getDiscount() + Constant.UNIT);
                }
                rechargeOptionVO.setRecommend("t".equals(rechargeOption.getRecommend()));
                options.add(rechargeOptionVO);
            }
            queryTopUpInfoResponse.setOptions(options);
        }
        return returnSuccess(queryTopUpInfoResponse);
    }

}
